<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>vue</title>
  <script src="../js/vue.js"></script>
</head>
<body>
  <div id="app">
    <my-table :data="stus">
      <template #header>
        <th>编号</th>
        <th>姓名</th>
        <th>年龄</th>
      </template>
      <template #body="{ item }">
        <td>{{item.id}}</td>
        <td>{{item.name}}</td>
        <td>{{item.age}}</td>
      </template>
    </my-table>
    <!-- 教师表格 -->
    <my-table :data="teachers">
      <template #header>
        <th>编号</th>
        <th>姓名</th>
        <th>性别</th>
        <th>薪资</th>
      </template>
      <template #body="{ item }">
        <td>{{item.id}}</td>
        <td>{{item.name}}</td>
        <td>{{item.gender}}</td>
        <td>{{item.salary}}</td>
      </template>
    </my-table>
  </div>
  <script>
    let myTable = {
      props: [ 'data' ],
      data(){
        return {

        }
      },
      template: `
        <div>
          <table>
            <thead>
              <tr>
                <slot name="header"></slot>
              </tr>
            </thead>
            <tbody>
              <tr v-for="item in data" :key="item.id">
                <slot name="body" :item="item"></slot>
              </tr>
            </tbody>
          </table>
        </div>
      `
    }
    Vue.component('my-table', myTable)
    let vm = new Vue({
      el: '#app',
      data: {
        stus: [
          { id: 1, name: '张三', age: 14},
          { id: 2, name: '李四', age: 16},
          { id: 3, name: '王五', age: 18},
        ],
        teachers: [
          { id: 1, name: '张老师', gender: 'male', salary: '6000'},
          { id: 2, name: '王老师', gender: 'female', salary: '7000'},
          { id: 3, name: '李老师', gender: 'male', salary: '8000'},
        ]
      }
    })
  </script>
</body>
</html>